//
//  FBIndicatorHUD.m
//  FBPersonalCenterProject
//
//  Created by Chenjing on 2017/7/3.
//  Copyright © 2017年 YunJing. All rights reserved.
//

#import "FBIndicatorHUD.h"
#import "MBProgressHUD.h"

#define DEF_KEYWINDOW [UIApplication sharedApplication].keyWindow

@interface FBIndicatorHUD ()

@end


@implementation FBIndicatorHUD

/** 初始化 */
+ (FBIndicatorHUD *)shareInstance{
    
    static FBIndicatorHUD *indicatorHUD;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        indicatorHUD = [[FBIndicatorHUD alloc] init];
    });
    return indicatorHUD;
}

+ (FBIndicatorConfig *)activityHUD {
    FBIndicatorConfig *config = [[FBIndicatorConfig alloc] init];
    config.type = FBIndicatorHUDTypeActivity;
    return config;
}

+ (FBIndicatorConfig *)animatedHUD {
    FBIndicatorConfig *config = [[FBIndicatorConfig alloc] init];
    config.type = FBIndicatorHUDTypeAnimated;
    return config;
}

#pragma mark - 默认菊花样式加载视图
+ (void)showActivityIndicatorWithMessage:(NSString *)message userInteractionEnabled:(BOOL)enabled inView:(UIView *)view{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.userInteractionEnabled = enabled;
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.removeFromSuperViewOnHide = YES;
    hud.minSize = CGSizeMake(78, 78);
    hud.contentColor = [UIColor whiteColor];
    hud.bezelView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
    hud.label.textColor = [UIColor colorWithRed:249/255.0f green:249/255.0f blue:249/255.0f alpha:1.0f];
    hud.label.font = [UIFont systemFontOfSize:12.0f];
    hud.label.text = message;
}

#pragma mark - 自定义动画加载视图
+ (void)showAnimatedIndicatorWithMessage:(NSString *)message userInteractionEnabled:(BOOL)enabled inView:(UIView *)view{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.userInteractionEnabled = enabled;
    hud.mode = MBProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    hud.margin = [self progressHUDMargin];
    hud.customView = [self animatedView];
    hud.label.textColor = [UIColor colorWithRed:45/255.0f green:42/255.0f blue:42/255.0f alpha:1.0f];
    hud.label.text = message;
}

//动画视图
+ (UIImageView *)animatedView {
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    imageView.animationImages = @[[UIImage imageNamed:@"FB_ProgressHUD_Image_One"],
                                  [UIImage imageNamed:@"FB_ProgressHUD_Image_Two"],
                                  [UIImage imageNamed:@"FB_ProgressHUD_Image_Three"]];
    imageView.animationDuration = 0.2f;
    [imageView startAnimating];
    return imageView;
}

//加载视图边框距离
+ (CGFloat)progressHUDMargin {
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat margin = 0;
    if (screenWidth <= 320) {
        margin = 8;
    } else if (screenWidth <= 375) {
        margin = 15;
    } else if (screenWidth <= 414) {
        margin = 20;
    } else {
        margin = 20;
    }
    return margin;
}

#pragma mark - 隐藏视图
+ (void)hidden {
    [MBProgressHUD hideHUDForView:DEF_KEYWINDOW animated:YES];
}

/**
 *  @breif  隐藏加载指示器
 */
+ (void)hiddenForView:(UIView *)view {
    [MBProgressHUD hideHUDForView:view animated:YES];
}

@end

@interface FBIndicatorConfigModel ()

@property (nonatomic, strong) UIView *superView;
@property (nonatomic , strong) NSString *indicatorMessage;
@property (nonatomic , assign) CGFloat margin;
@property (nonatomic , assign) BOOL userInteractionEnabled;
@property (nonatomic , copy) void (^modelFinishConfig)();


@end

@implementation FBIndicatorConfigModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.userInteractionEnabled = NO;
        
    }
    return self;
}

- (ConfigBOOL)UserEnabled {
    __weak typeof(self) weakSelf = self;
    
    return ^(BOOL enabled){
        if (weakSelf) {
            weakSelf.userInteractionEnabled = enabled;
        }
        return weakSelf;
    };
}

- (ConfigString)Message {
    __weak typeof(self) weakSelf = self;
    return ^(NSString *message){
        if (weakSelf) {
            weakSelf.indicatorMessage = message;
        }
        return weakSelf;
    };
}

- (ConfigFloat)MarginWidth {
    __weak typeof(self) weakSelf = self;
    return ^(CGFloat marginWidth){
        if (weakSelf) {
            weakSelf.margin = marginWidth;
        }
        return weakSelf;
    };
}

- (ConfigShow)Show {
    __weak typeof(self) weakSelf = self;
    
    return ^(){
        if (weakSelf) {
            self.superView = DEF_KEYWINDOW;
            if (weakSelf.modelFinishConfig) {
                weakSelf.modelFinishConfig();
            }
        }
        return weakSelf;
    };
}

- (ConfigShowInView)ShowInView {
    __weak typeof(self) weakself = self;
    return ^(UIView *showInView){
        if (weakself) {
            weakself.superView = showInView;
            if (weakself.modelFinishConfig) {
                weakself.modelFinishConfig();
            }
        }
        return weakself;
    };
}

@end

@implementation FBIndicatorConfig

- (void)dealloc{
    
    _config = nil;
}

- (instancetype)init
{
    self = [super init];
    
    if (self) {
        
        __weak typeof(self) weakSelf = self;
        
        self.config.modelFinishConfig = ^{
            
            __strong typeof(weakSelf) strongSelf = weakSelf;
            
            if (!strongSelf) return;
            
            [strongSelf showIndicatorView];
        };
    }
    return self;
}

- (void)setType:(FBIndicatorHUDType)type {
    _type = type;
    switch (type) {
        case FBIndicatorHUDTypeActivity:
            break;
        case FBIndicatorHUDTypeAnimated:
            break;
        default:
            break;
    }
}

- (void)show {
    
    self.config.superView = DEF_KEYWINDOW;
    [self showIndicatorView];
}

- (void)showInView:(UIView *)view{
    
    self.config.superView = view;
    [self showIndicatorView];
}


- (void)showIndicatorView {
    if (!self.config.superView) {
        NSLog(@"View must not be nil.");
        return;
    }
    switch (self.type) {
        case FBIndicatorHUDTypeActivity:
            [FBIndicatorHUD showActivityIndicatorWithMessage:self.config.indicatorMessage userInteractionEnabled:self.config.userInteractionEnabled inView:self.config.superView];
            break;
        case FBIndicatorHUDTypeAnimated:
            [FBIndicatorHUD showAnimatedIndicatorWithMessage:self.config.indicatorMessage userInteractionEnabled:self.config.userInteractionEnabled inView:self.config.superView];
            break;
        default:
            break;
    }
}

#pragma mark - LazyLoading
- (FBIndicatorConfigModel *)config {
    
    if (!_config) _config = [[FBIndicatorConfigModel alloc] init];
    
    return _config;
}

@end
